package com.itheima.goods.controller.admin;


import com.itheima.goods.dto.CategoryDTO;
import com.itheima.goods.dto.CategoryPageQueryDTO;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.service.CategoryService;
import com.itheima.result.PageBean;
import com.itheima.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /**
     * 查询分类
     * @param categoryPageQueryDTO
     * @return
     */

    @GetMapping("/page")
    public Result pageSelect(CategoryPageQueryDTO categoryPageQueryDTO){
        PageBean pageBean= categoryService.page(categoryPageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 设置起售还是停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result get(@PathVariable Integer status, Long id){

        categoryService.setStatus(status,id);
        return Result.success();
    }

    /**
     * 添加分类
     * @param categoryDTO
     * @return
     */

    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.addCategory(categoryDTO);
        return Result.success();

    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Long id){

       Result result= categoryService.deleteById(id);
        return result;
    }

    /**
     * 根据类型查询分类
     * @param categoryDTO
     * @return
     */
    @GetMapping("/list")
    public Result list(CategoryDTO categoryDTO){
       List<Category> categories= categoryService.select(categoryDTO);
        return Result.success(categories);
    }
}
